"""
业绩管理模型
"""
from sqlalchemy import Column, Integer, Numeric, Enum, ForeignKey, DateTime, func, String
from sqlalchemy.orm import relationship
from .base import Base


class SalesPerformance(Base):
    """业绩统计表（售前部门）"""
    __tablename__ = "sales_performance"
    
    employee_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="员工ID")
    performance_date = Column(DateTime, nullable=False, comment="业绩日期")
    daily_sales = Column(Numeric(10, 2), default=0.00, comment="日业绩")
    weekly_sales = Column(Numeric(10, 2), default=0.00, comment="周业绩")
    monthly_sales = Column(Numeric(10, 2), default=0.00, comment="月业绩")
    order_count = Column(Integer, default=0, comment="订单数量")
    conversion_rate = Column(Numeric(5, 2), default=0.00, comment="转化率")
    
    # 关系
    employee = relationship("Employee", back_populates="sales_performance")
    
    def __repr__(self):
        return f"<SalesPerformance(id={self.id}, employee_id={self.employee_id}, performance_date={self.performance_date})>"


class TrafficAllocation(Base):
    """流量分配表（运营部门）"""
    __tablename__ = "traffic_allocations"
    
    traffic_channel_id = Column(Integer, ForeignKey("traffic_channels.id"), nullable=False, comment="流量渠道ID")
    sales_employee_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="分配的售前员工ID")
    allocation_date = Column(DateTime, nullable=False, comment="分配日期")
    traffic_count = Column(Integer, default=0, comment="分配流量数量")
    status = Column(Enum("active", "completed", "cancelled", name="allocation_status"), 
                   default="active", comment="状态")
    
    # 关系
    traffic_channel = relationship("TrafficChannel", back_populates="traffic_allocations")
    sales_employee = relationship("Employee", back_populates="traffic_allocations")
    
    def __repr__(self):
        return f"<TrafficAllocation(id={self.id}, traffic_channel_id={self.traffic_channel_id}, sales_employee_id={self.sales_employee_id})>"


class TrafficStatistics(Base):
    """流量统计表（售前每日填写）"""
    __tablename__ = "traffic_statistics"
    
    reception_employee_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="接待员工ID")
    statistics_date = Column(DateTime, nullable=False, comment="统计日期")
    region_id = Column(Integer, ForeignKey("regions.id"), comment="地区ID")
    subject_id = Column(Integer, ForeignKey("subjects.id"), comment="科目ID")
    team_id = Column(Integer, ForeignKey("teams.id"), comment="团队ID")
    team_leader_id = Column(Integer, ForeignKey("employees.id"), comment="团队负责人ID")
    traffic_channel_id = Column(Integer, ForeignKey("traffic_channels.id"), comment="渠道ID")
    operation_employee_id = Column(Integer, ForeignKey("employees.id"), comment="运营员工ID")
    source = Column(String(50), comment="来源")
    traffic_count = Column(Integer, default=0, comment="流量数量")
    reply_count = Column(Integer, default=0, comment="回复数量")
    invite_count = Column(Integer, default=0, comment="邀课数量")
    reply_rate = Column(Numeric(5, 2), default=0.00, comment="回复率")
    invite_rate = Column(Numeric(5, 2), default=0.00, comment="邀课率")
    new_registration_count = Column(Integer, default=0, comment="新报名数量")
    new_registration_amount = Column(Numeric(10, 2), default=0.00, comment="新报名业绩")
    old_traffic_registration_count = Column(Integer, default=0, comment="老量报名数量")
    old_traffic_registration_amount = Column(Numeric(10, 2), default=0.00, comment="老量业绩")
    interest_count = Column(Integer, default=0, comment="利息")
    total_registration_count = Column(Integer, default=0, comment="总报名数量")
    refund_count = Column(Integer, default=0, comment="退款数量")
    
    # 关系
    reception_employee = relationship("Employee", foreign_keys=[reception_employee_id], back_populates="traffic_statistics")
    region = relationship("Region", back_populates="traffic_statistics")
    subject = relationship("Subject", back_populates="traffic_statistics")
    team = relationship("Team", back_populates="traffic_statistics")
    team_leader = relationship("Employee", foreign_keys=[team_leader_id])
    traffic_channel = relationship("TrafficChannel", back_populates="traffic_statistics")
    operation_employee = relationship("Employee", foreign_keys=[operation_employee_id])
    
    def __repr__(self):
        return f"<TrafficStatistics(id={self.id}, reception_employee_id={self.reception_employee_id}, statistics_date={self.statistics_date})>"
